import re
from datetime import datetime
from collections import defaultdict

# 日志文件路径
log_file = 'your_log_file_path.log'

# 用于存储每个应用的开始时间
start_times = {}
# 用于存储所有应用的总时间和调用次数
total_time = 0
total_count = 0

# 解析日志文件
with open(log_file, 'r') as file:
    for line in file:
        # 使用正则表达式匹配日志中的时间、操作（开始或结束）和应用名
        match = re.search(r'(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}).*GetBundleStats,(begin|end) to GetBundleStats,bundleName is :(\S+)', line)
        if match:
            time_str, operation, app_name = match.groups()
            # 将时间字符串转换为datetime对象
            time = datetime.strptime(time_str, '%m-%d %H:%M:%S.%f')
            if operation == 'begin':
                # 如果是开始操作，记录开始时间
                start_times[app_name] = time
            elif operation == 'end' and app_name in start_times:
                # 如果是结束操作，计算时间差并累加到总时间，然后增加调用次数
                delta = time - start_times[app_name]
                total_time += delta.total_seconds()
                total_count += 1

# 计算所有应用的平均时间
avg_time = total_time / total_count if total_count else 0
print(f'Average time for all applications: {avg_time} seconds')